package nsoft.swing.window;

import java.awt.BorderLayout;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import javax.swing.JPanel;

import nsoft.swing.engine.ContainerFactory;
import nsoft.util.UiModel;

public class WebUiManager extends UiManager {

	private JPanel contentPane;

	private StatuBar statuBar;

	private Stack<UiModel> previousViews;

	private Stack<UiModel> nextViews;

	private UiModel currentView;

	@Override
	public boolean activateWindow(String screenId) {/*
													 * if(currentView.getScreenId().equals(screenId)){
													 * return true; }
													 * AbstractView<?> view =
													 * openViews.get(screenId);
													 * if(view!=null){
													 * if(currentView!=null){
													 * contentPane.remove(view); }
													 * contentPane.add(view,BorderLayout.CENTER);
													 * //contentPane.revalidate();
													 * contentPane.repaint();
													 * currentView = view;
													 * previousViews.push(view);
													 * return true; } return
													 * false;
													 */
		return false;
	}

	@Override
	public void openWindow(String screenId, UiModel model) {
		statuBar.setStatu(10);
		previousViews.push(currentView);
		statuBar.setTitle("Loading... :"+screenId);
		contentPane.add(createView(model),BorderLayout.CENTER);
		currentView = model;
		statuBar.setTitle(screenId);
		statuBar.setStatu(90);
	}

	@Override
	public UiType getUiType() {
		return UiType.UI_TYPE_WEB;
	}

	@Override
	public void applicationClosing() {
		List<String> screenIds = new ArrayList<String>();
		for (String screenId : openViews.keySet()) {
			screenIds.add(screenId);
		}
		for (String screenId : screenIds) {
			closeWindow(screenId);
		}
	}

	@Override
	public boolean closeWindow(String screenId) {/*
													 * UiModel view =
													 * openViews.get(screenId);
													 * if(view!=null){
													 * view.closingView();
													 * if(view==currentView){
													 * contentPane.remove(view); }
													 * nextViews.remove(view);
													 * previousViews.remove(view);
													 * openViews.remove(screenId);
													 * return true; } return
													 * false;
													 */
		return false;
	}

	@Override
	public void init() {
		setBuilder(new ContainerFactory());
		contentPane = new JPanel(new BorderLayout());
		statuBar = new StatuBar();
		statuBar.setStatu(100);
		previousViews = new Stack<UiModel>();
		nextViews = new Stack<UiModel>();
		contentPane.add(statuBar, BorderLayout.PAGE_END);

	}

	@Override
	public Container getRootPane() {
		return contentPane;
	}
}
